home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_100 / 198_02 / termio.c < prev    next >
C/C++ Source or Header  |  1990-01-21  |  19KB  |  731 lines

  1. /*
  2.  * The functions in this file negotiate with the operating system for
  3.  * characters, and write characters in a barely buffered fashion on the display.
  4.  * All operating systems.
  5.  */
  6. #include        <stdio.h>
  7. #include    "estruct.h"
  8. #include        "edef.h"
  9.  
  10. #if   MSDOS & TURBO
  11. #include <conio.h>
  12. #endif
  13.  
  14. #if     AMIGA
  15. #define NEW 1006L
  16. #define AMG_MAXBUF      1024L
  17. static long terminal;
  18. static char     scrn_tmp[AMG_MAXBUF+1];
  19. static long     scrn_tmp_p = 0;
  20. #endif
  21.  
  22. #if ST520 & MEGAMAX
  23. #include <osbind.h>
  24.     int STscancode = 0;    
  25. #endif
  26.  
  27. #if     VMS
  28. #include        <stsdef.h>
  29. #include        <ssdef.h>
  30. #include        <descrip.h>
  31. #include        <iodef.h>
  32. #include        <ttdef.h>
  33. #include    <tt2def.h>
  34.  
  35. #define EFN     0                       /* Event flag                   */
  36.  
  37. char    obuf[NOBUF];                    /* Output buffer                */
  38. int     nobuf;                  /* # of bytes in above    */
  39. char    ibuf[NIBUF];                    /* Input buffer          */
  40. int     nibuf;                  /* # of bytes in above  */
  41. int     ibufi;                  /* Read index                   */
  42. int     oldmode[3];                     /* Old TTY mode bits            */
  43. int     newmode[3];                     /* New TTY mode bits            */
  44. short   iochan;                  /* TTY I/O channel             */
  45. #endif
  46.  
  47. #if     CPM
  48. #include        <bdos.h>
  49. #endif
  50.  
  51. #if     MSDOS & (LATTICE | MSC | TURBO | AZTEC | MWC86 | C86)
  52. union REGS rg;        /* cpu register for use of DOS calls */
  53. int nxtchar = -1;    /* character held from type ahead    */
  54. #endif
  55.  
  56. #if RAINBOW
  57. #include "rainbow.h"
  58. #endif
  59.  
  60. #if    USG            /* System V */
  61. #include    <signal.h>
  62. #include    <termio.h>
  63. #include    <fcntl.h>
  64. struct    termio    otermio;    /* original terminal characteristics */
  65. struct    termio    ntermio;    /* charactoristics to use inside */
  66.  
  67. #if MSC
  68. /* #include <sgtty.h> */    /* not needed */
  69. #undef FIOCLEX
  70. #undef FIONCLEX
  71. #include <sys/ioctl.h>
  72.  
  73. int tafd;            /* extra fd opened with no wait    */
  74. int fcstat;            /* saved fcntl status        */
  75. #define    NIBUF    63
  76. char    ibuf[NIBUF];            /* Input buffer          */
  77. int     nibuf = 0;              /* # of bytes in above  */
  78. int     ibufi = 0;              /* Read index                   */
  79. #else
  80. int kbdflgs;            /* saved keyboard fd flags    */
  81. int kbdpoll;            /* in O_NDELAY mode            */
  82. int kbdqp;            /* there is a char in kbdq    */
  83. char kbdq;            /* char we've already read    */
  84. #endif
  85. #endif
  86.  
  87. #if V7 | BSD
  88. #undef    CTRL
  89. #include        <sgtty.h>        /* for stty/gtty functions */
  90. #include    <signal.h>
  91. struct  sgttyb  ostate;          /* saved tty state */
  92. struct  sgttyb  nstate;          /* values for editor mode */
  93. struct tchars    otchars;    /* Saved terminal special character set */
  94. struct tchars    ntchars = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
  95. #endif
  96.                 /* A lot of nothing */
  97. #if BSD
  98. #include <sys/ioctl.h>        /* to get at the typeahead */
  99. extern    int rtfrmshell();    /* return from suspended shell */
  100. #if BSD29
  101. #else
  102. #define    TBUFSIZ    128
  103. char tobuf[TBUFSIZ];        /* terminal output buffer */
  104. #endif
  105. #endif
  106.  
  107. extern int termflag;
  108.  
  109. /*
  110.  * This function is called once to set up the terminal device streams.
  111.  * On VMS, it translates TT until it finds the terminal, then assigns
  112.  * a channel to it and sets it raw. On CPM it is a no-op.
  113.  */
  114. ttopen()
  115. {
  116. #if     AMIGA
  117.     char oline[NSTRING];
  118. #if    AZTEC
  119.     extern    Enable_Abort;    /* Turn off ctrl-C interrupt */
  120.  
  121.     Enable_Abort = 0;    /* for the Manx compiler */
  122. #endif
  123.     strcpy(oline, "RAW:0/0/640/200/");
  124.     strcat(oline, PROGNAME);
  125.     strcat(oline, " ");
  126.     strcat(oline, VERSION);
  127.     strcat(oline, "/Amiga");
  128.         terminal = Open(oline, NEW);
  129. #endif
  130. #if     VMS
  131.         struct  dsc$descriptor  idsc;
  132.         struct  dsc$descriptor  odsc;
  133.         char    oname[40];
  134.         int     iosb[2];
  135.         int     status;
  136.  
  137.         odsc.dsc$a_pointer = "TT";
  138.         odsc.dsc$w_length  = strlen(odsc.dsc$a_pointer);
  139.         odsc.dsc$b_dtype        = DSC$K_DTYPE_T;
  140.         odsc.dsc$b_class        = DSC$K_CLASS_S;
  141.         idsc.dsc$b_dtype        = DSC$K_DTYPE_T;
  142.         idsc.dsc$b_class        = DSC$K_CLASS_S;
  143.         do {
  144.                 idsc.dsc$a_pointer = odsc.dsc$a_pointer;
  145.                 idsc.dsc$w_length  = odsc.dsc$w_length;
  146.                 odsc.dsc$a_pointer = &oname[0];
  147.                 odsc.dsc$w_length  = sizeof(oname);
  148.                 status = LIB$SYS_TRNLOG(&idsc, &odsc.dsc$w_length, &odsc);
  149.                 if (status!=SS$_NORMAL && status!=SS$_NOTRAN)
  150.                         exit(status);
  151.                 if (oname[0] == 0x1B) {
  152.                         odsc.dsc$a_pointer += 4;
  153.                         odsc.dsc$w_length  -= 4;
  154.                 }
  155.         } while (status == SS$_NORMAL);
  156.         status = SYS$ASSIGN(&odsc, &iochan, 0, 0);
  157.         if (status != SS$_NORMAL)
  158.                 exit(status);
  159.  
  160.     /* sense mode returns:
  161.      *    mode[0] :0-7 = class, :8-15 = type, :16-31 = width
  162.      *    mode[1] :0-23 = characteristics, :24-31 = length
  163.      */
  164.  
  165.         status = SYS$QIOW(EFN, iochan, IO$_SENSEMODE, iosb, 0, 0,
  166.                           oldmode, sizeof(oldmode), 0, 0, 0, 0);
  167.         if (status!=SS$_NORMAL || (iosb[0]&0xFFFF)!=SS$_NORMAL)
  168.                 exit(status);
  169.         newmode[0] = oldmode[0];
  170.         newmode[1] = oldmode[1] /* | TT$M_NOECHO */ ;
  171.         newmode[2] = oldmode[2];
  172.     if (!termflag) {
  173.             newmode[1] &= ~(TT$M_TTSYNC|TT$M_HOSTSYNC);
  174.             newmode[2] |= TT2$M_PASTHRU;
  175.     }
  176.         status = SYS$QIOW(EFN, iochan, IO$_SETMODE, iosb, 0, 0,
  177.                           newmode, sizeof(newmode), 0, 0, 0, 0);
  178.         if (status!=SS$_NORMAL || (iosb[0]&0xFFFF)!=SS$_NORMAL)
  179.                 exit(status);
  180.         term.t_nrow = (newmode[1]>>24) - 1;
  181.     if (term.t_nrow > term.t_mrow) term.t_mrow = term.t_nrow;
  182.         term.t_ncol = newmode[0]>>16;
  183.     if (term.t_ncol > term.t_mcol) term.t_mcol = term.t_ncol;
  184. #endif
  185. #if     CPM
  186. #endif
  187.  
  188. #if     MSDOS & (HP150 == 0) & LATTICE
  189.     /* kill the ctrl-break interupt */
  190.     rg.h.ah = 0x33;        /* control-break check dos call */
  191.     rg.h.al = 1;        /* set the current state */
  192.     rg.h.dl = 0;        /* set it OFF */
  193.     intdos(&rg, &rg);    /* go for it! */
  194. #endif
  195.  
  196. #if    USG
  197.     ioctl(0, TCGETA, &otermio);    /* save old settings */
  198.     ntermio.c_oflag = 0;        /* setup new settings */
  199.     if (termflag)
  200.         ntermio.c_iflag = otermio.c_iflag & (IXON|IXANY|IXOFF);
  201.     else
  202.         ntermio.c_iflag = 0;
  203.     ntermio.c_cflag = otermio.c_cflag;
  204.     ntermio.c_lflag = 0;
  205.     ntermio.c_line = otermio.c_line;
  206.     ntermio.c_cc[VMIN] = 1;
  207.     ntermio.c_cc[VTIME] = 0;
  208.     ioctl(0, TCSETAW, &ntermio);    /* and activate them */
  209. #ifdef    TCXONC
  210.     /* ioctl(0, TCXONC, 1);    */    /* restart suspended (^S'ed) output */
  211. #endif
  212. #if    MSC
  213.     tafd = open("/dev/tty", O_RDONLY, 0);
  214.     ioctl(tafd, TCSETA, &ntermio);    /* and activate them */
  215.     fcstat = fcntl(tafd, F_GETFL, 0);
  216.     fcntl(tafd, F_SETFL, fcstat | O_NDELAY);
  217. #else
  218.     kbdflgs = fcntl( 0, F_GETFL, 0 );
  219.     kbdpoll = FALSE;
  220. #endif
  221. #endif
  222.  
  223. #if     V7 | BSD
  224.         gtty(0, &ostate);                       /* save old state */
  225.         gtty(0, &nstate);                       /* get base of new state */
  226.         nstate.sg_flags |= RAW;
  227.         nstate.sg_flags &= ~(ECHO|CRMOD);       /* no echo for now... */
  228.         stty(0, &nstate);                       /* set mode */
  229.     ioctl(0, TIOCGETC, &otchars);        /* Save old characters */
  230.     ioctl(0, TIOCSETC, &ntchars);        /* Place new character into K */
  231. #endif
  232. #if    BSD
  233. #if    BSD29
  234. #else
  235.     /* provide a smaller terminal output buffer so that
  236.        the type ahead detection works better (more often) */
  237.     setbuffer(stdout, &tobuf[0], TBUFSIZ);
  238. #ifdef    _IONBF
  239.     setvbuf(stdin, NULL, _IONBF, 0);
  240. #endif
  241. #endif
  242.     signal(SIGTSTP,SIG_DFL);    /* set signals so that we can */
  243.     signal(SIGCONT,rtfrmshell);    /* suspend & restart emacs */
  244. #endif
  245.     /* on all screens we are not sure of the initial position
  246.        of the cursor                    */
  247.     ttrow = 999;
  248.     ttcol = 999;
  249. }
  250.  
  251. /*
  252.  * This function gets called just before we go back home to the command
  253.  * interpreter. On VMS it puts the terminal back in a reasonable state.
  254.  * Another no-operation on CPM.
  255.  */
  256. ttclose()
  257. {
  258. #if     AMIG